$(function () {
    //初始化加载粘贴板
    var clipboard = new Clipboard('.copyLink');
    var _ele = $("#shareId").val();
    var ele = $("#contentId").val();
    $("body").on('click','.setting , .update',function () {

        var _v = $(this).attr('value');
        parent.layer.open({
            type:1,
            area:['640px','450px'],
            skin:'layui-layer-demo',
            title:_v == 1 ? '分享自定义' : '关注公众号设置',
            content: _v == 1 ? _ele : ele,
            btn:['保存','取消'],
            yes:function (index,layer) {
                var params = parent.$("#saveAccount").serializeJSON();
                var describe = parent.$('#describe').text();
                params.description = describe;
                params = JSON.stringify(params);
                $.ajax({
                    url: baseloaction + '/admin/netSchoolConfig/saveAccountConfig',
                    type:"post",
                    dataType:'json',
                    data:params,
                    contentType:'application/json;charset=utf-8',
                    success:function (result) {
                        if(result.success==true){
                            icModale(0, result.message);
                            window.location.href= baseloaction + '/admin/netSchoolConfig/accountConfig';
                            parent.layer.close(index);
                        }else{
                            icModale(0, result.message);
                        }
                    },
                    error:function (error) {
                        icModale(0, "系统错误，配置数据保存失败");
                    }
                });
            }
        });
        parent.$('#imgUploadBtn').on('click',function (e) {
            e.preventDefault();
            initUpdateFileWin({
                fileType:'img',//文类类型["img","ico","xls","pdf","zip"]
                callback:'_uploadLogoCallback',
                cut:false//不开启剪切
            });
        })
    }).on('click','.copyLink',function () {//h5平台地址复制
        clipboard.text = function() {
            return $('.config-text').text();
        };
        clipboard.on('error', function() {
            icModale(0, '复制链接失败');
        });
        clipboard.on('success', function() {
            icModale(0, '复制链接成功');
        });
    }).on("ifClicked",'input[type=checkbox]', function (event) {

        var configStatus ='';
        var id = this.id;
        if(this.id == 1){
            if (this.checked == false){
                configStatus = 0;
                $('.shareHide').removeClass('hide');
            }else{
                configStatus = 1;
                $('.shareHide').addClass('hide');
            }
            //console.log(this.checked);
        }
        if(this.id == 2){
            if (this.checked == false){
                configStatus = 0;
                $('.accountHide').removeClass('hide');
            }else{
                configStatus = 1;
                $('.accountHide').addClass('hide');
            }
        }
        saveShareAccount(id,configStatus);
    }).on("click",".toConfig",function () {
        var configId = $('#toConfig').val();
        var url = "/admin/manage/config/initUpdateConfig?id="+configId;
        getRedirectMenu(url);
    }).on("click","#saveConfig",function () {
        var params = $("#weixinform").serializeJSON();
        params = JSON.stringify(params);
        $.ajax({
            url: baseloaction + '/admin/manage/config/saveConfig',
            type:"post",
            dataType:'json',
            data:params,
            contentType:'application/json;charset=utf-8',
            success:function (result) {
                icModale(0, result.message);
            },
            error:function (error) {
                icModale(0, "系统错误，配置数据保存失败");
            }
        });
    }).on("click","#wechatServiceBtn",function () {//LOGO图片上传 (128px * 128px)
        //初始化图片上传窗口
        initUpdateFileWin({
            fileType:'img',//文类类型["img","ico","xls","pdf","zip"]
            callback:'uploadWechatServicCallback',
            cut:false//不开启剪切
        });
    })



    function saveShareAccount(id,configStatus) {
        $.ajax({
            url: baseloaction + '/admin/netSchoolConfig/updateAccountConfig',
            type:"post",
            dataType:'json',
            data:{
                'id':id,
                'configStatus':configStatus
            },
            success:function (result) {
                icModale(0, result.message);
            },
            error:function (error) {
                icModale(0, "系统错误，配置数据保存失败");
            }
        });
    }

});

/**
 * 上传Logo回调
 * @param data
 */
function _uploadLogoCallback(data) {
    parent.$("#logoUrl").val(data.url);
    parent.$(".logo-img").attr("src",data.url);
}/**
 * 上传Logo回调
 * @param data
 */
function uploadWechatServicCallback(data) {
    $("#wechatService-img").parent().removeClass("hide");
    $("#wechatServiceUrl").val(data.url);
    $("#wechatService-img").attr("src",data.url);
}